#pragma once

#include "Component.h"
#include "Resources.h"
#include "Circle.h"
#include "CustomRectangle.h"

/**
* @file Sprite.h
* @brief Base class for drawable elements
*/
class Sprite : public Component
{
public:
	Sprite();
	Sprite(const std::string& path);
	Sprite(const std::string& path, int x, int y);
	Sprite(const std::string& path, const Vector* const v);
	Sprite(const std::string& path, int x, int y, int w, int h);
	Sprite(const std::string& path, const Vector* const v, int w, int h);
	Sprite(const std::string& path, const SDL_Rect& dstRect);
	Sprite(const std::string& path, const SDL_Rect& dstRect, const SDL_Rect& srcRect);

	Sprite(SDL_Texture* tex);
	Sprite(SDL_Texture* tex, int x, int y);
	Sprite(SDL_Texture* tex, int x, int y, int w, int h);
	Sprite(SDL_Texture* tex, const SDL_Rect& rect);
	Sprite(SDL_Texture* tex, const SDL_Rect& dstRect, const SDL_Rect& srcRect);

	virtual ~Sprite();

	// Getters
	const SDL_Rect& GetRect() { return dstRect; }
	const SDL_Rect& GetSrcRect() { return srcRect; }
	bool GetVisible() { return isVisible; }
	Polygon* GetCollider() { return collider; }

	// Setters
	void SetFlip(SDL_RendererFlip state){ flipState = state; }
	void SetDstRect(const SDL_Rect& r) { this->dstRect = r; }
	void SetSrcRect(const SDL_Rect& r) { this->srcRect = r; }
	void SetPosition(int x, int y)
	{
		dstRect.x = x;
		dstRect.y = y;
		if (isCircular) {
			collider->SetPosition((float)(x + dstRect.w / 2), (float)(y + dstRect.h / 2));
		} else {
			collider->SetPosition((float)x, (float)y);
		}
	}
	void SetPosition(const Vector* const v) { SetPosition(v->x, v->y); }
	void SetRotationAngle(float angle) { this->angle = angle; }
	void SetTexture(const std::string& path) 
	{ 
		tex = Resources::GetInstance()->GetTexture(path);
		SDL_QueryTexture(tex, 0, 0, &srcRect.w, &srcRect.h);
		SetSrcRect(this->srcRect);
		SetDstRect(this->srcRect);
	}
	void SetTexture(SDL_Texture* texture) 
	{
		tex = texture; 
		SDL_QueryTexture(tex, 0, 0, &srcRect.w, &srcRect.h);
		SetSrcRect(this->srcRect);
		SetDstRect(this->srcRect);
	}
	void SetTexture(SDL_Texture* texture, SDL_Rect refRect)
	{
		tex = texture;
		SDL_QueryTexture(tex, 0, 0, &srcRect.w, &srcRect.h);
		SetSrcRect(this->srcRect);
		SetDstRect(this->srcRect);
		SetPosition(refRect.x, refRect.y);
	}
	void SetVisible(bool visible) { isVisible = visible; }
	void SetCircular(bool circular) { isCircular = circular; }
	void SetCollider(Polygon *col) { collider = col; }

	virtual void Start();
	virtual void Update(float dt);
	virtual void Draw();
	virtual void Stop();

protected:
	void ApplySurface(SDL_Renderer* renderer);
	bool isVisible;
	bool isCircular;
	float angle;
	SDL_Rect dstRect;
	SDL_Rect srcRect;
	SDL_Texture* tex;
	SDL_RendererFlip flipState;
	Polygon* collider;
};